package com.z8fz.admin.food.dao.impl;

import org.springframework.stereotype.Repository;

import com.z8fz.admin.food.dao.FoodMethodDao;
import com.z8fz.admin.food.model.Food;
import com.z8fz.admin.food.model.Method;
import com.z8fz.common.BaseDao;
import com.z8fz.common.pager.PageModel;

@Repository
public class FoodMethodDaoImpl extends BaseDao implements FoodMethodDao {
	public void insert(Method method) {
		Food food = new Food();
		food.setId(method.getFoodId());
		method.setTbFood(food);
		this.getHibernateTemplate().save(method);
	}

	public int selectMaxStep(Integer id) {
		String hql = "select max(t.foodStep) from Method t where t.tbFood="
				+ id;
		Integer maxStep = (Integer) getSession().createQuery(hql)
				.uniqueResult();
		if (maxStep == null)
			return 1;
		else
			return maxStep + 1;
	}

	public PageModel selectMethods(Integer id) { 
		String hql = "from Method t  where t.tbFood=" + id;
		return searchpaginated(hql);
	}

	public void insertReadCount(Integer id) {
		String hql = "update Food t set t.readCount =t.readCount+1 where t.id="
				+ id;
		getSession().createQuery(hql).executeUpdate();

	}
}
